﻿package com.tudou.home.model
{
    import com.tudou.cookie.*;
    import com.tudou.home.commands.*;
    import com.tudou.home.consts.*;
    import com.tudou.home.track.*;
    import com.tudou.newPlayer.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import com.tudou.v2.*;
    import flash.events.*;
    import flash.utils.*;
    import org.puremvc.as3.interfaces.*;
    import org.puremvc.as3.patterns.facade.*;

    public class PlayerProxy extends TudouPlayer implements IProxy
    {
        private var cacheStateChanged:String;
        private var hasFirstMetadata:Boolean = false;
        private var showFailCount:int;
        private var showBuffer:Boolean;
        private var _delayTimer:Timer;
        private var changeVideoCount:int;
        private var _isLoadComplete:Boolean = false;
        protected var facade:IFacade;
        protected var data:Object;
        private var _fullCached:Boolean;
        private var bufferCount:int;
        private var playerEventTracker:PlayerEventTracker;
        public var previewPlayer:PreviewPlayer;
        private var showFailure:Boolean;
        private var pageInfo:PageInfo;
        private var _isRequestComplete:Boolean = false;
        private var hasSendNearComplete:Boolean = false;
        protected var playerTimer:Timer;
        private var nearCompleteTime:int = 5;
        private var _delayTime:Number;
        private var showBufferCount:int;
        private var onBuffer:Boolean;
        private static const CHANGE_VIDEO_MAX_COUNT:uint = 200;
        public static const NAME:String = "PlayerProxy";
        static const PLAYER_CLOCK:Number = 50;
        private static const SHOW_FAIL_MAX_COUNT:uint = 220;
        private static const SHOW_BUFFER_MAX_COUNT:uint = 60;
        private static const BUFFER_MAX_COUNT:uint = 3;

        public function PlayerProxy()
        {
            facade = Facade.getInstance();
            super(PlayerConsts.DEFAULT_VIDEO_WIDTH, PlayerConsts.DEFAULT_VIDEO_HEIGHT);
            pageInfo = PageInfo.getInstance();
            this.iBaseInfo = pageInfo;
            alwaysCheckPolicyFile = true;
            playerEventTracker = PlayerEventTracker.getInstance();
            initialize();
            reset();
            return;
        }// end function

        private function addNodeLog(param1:String, param2:Array = null) : void
        {
            AOPTracker.getInst().addNodeLog(param1, param2);
            UserLog.getInst().addNode(param1, param2);
            return;
        }// end function

        public function set delayTime(param1:Number) : void
        {
            _delayTime = param1;
            _delayTimer.reset();
            _delayTimer.start();
            return;
        }// end function

        private function bufferFullHlr() : void
        {
            showFailure = false;
            playerEventTracker.sendStartEventTrack(URLUtil.getDomainName(playerStatus.playURL), playerStatus.totalTime);
            sendNotification(PlayerCommandSet.FIRST_BUFFER_FULL);
            return;
        }// end function

        protected function skipTailHlr(event:PlayerEvent) : void
        {
            sendNotification(PlayerCommandSet.SKIPPING_TAIL);
            return;
        }// end function

        private function initialize() : void
        {
            _delayTimer = new Timer(1000);
            playerTimer = new Timer(PLAYER_CLOCK);
            playerTimer.addEventListener(TimerEvent.TIMER, playerTimerHlr);
            player.addEventListener(PlayerEvent.CHANGE_STREAM, changeStreamHlr);
            player.addEventListener(PlayerEvent.STREAM_BUFFER_FULL, streamBufferFullHlr);
            player.addEventListener(PlayerEvent.STREAM_BUFFER_EMPTY, streamBufferEmptyHlr);
            player.addEventListener(PlayerEvent.SKIP_HEAD, skipHeadHlr);
            player.addEventListener(PlayerEvent.SKIP_TAIL, skipTailHlr);
            previewPlayer = new PreviewPlayer();
            previewPlayer.addEventListener(PreviewPlayer.READY, previewReadyHlr);
            return;
        }// end function

        public function sendNotification(param1:String, param2:Object = null, param3:String = null) : void
        {
            facade.sendNotification(param1, param2, param3);
            return;
        }// end function

        private function changeStreamHlr(event:PlayerEvent) : void
        {
            trace("播放新地址：", event.data);
            _isRequestComplete = false;
            pageInfo.updatePpid();
            FastTudouTracker.send(pageInfo, playerStatus.cdnURL, playerStatus.playTime);
            addNodeLog(NodeLog.VIDEO_REQUEST_START, [encodeURIComponent(playerStatus.playURL)]);
            return;
        }// end function

        public function get fullCached() : Boolean
        {
            return _fullCached;
        }// end function

        override public function set videoInfo(param1:VideoInfo) : void
        {
            _videoInfo = param1;
            if (PlayerConsts.NOAD_USERS.indexOf(pageInfo.userId) >= 0)
            {
                player.setMasks(null);
            }
            else
            {
                player.setMasks(TempConsts.maskFilter(pageInfo.iid, _videoInfo.logoMaskList));
            }
            nextStep();
            return;
        }// end function

        override protected function metaDataHlr(event:PlayerEvent) : void
        {
            currMetaData = player.metaData;
            if (!hasFirstMetadata)
            {
                hasFirstMetadata = true;
                addNodeLog(NodeLog.METADATA_WIDTH_HEIGHT, [currMetaData.width + ":" + currMetaData.height]);
            }
            var _loc_2:* = currVideoNodeInfo;
            previewPlayer.init(_loc_2.url, player.metaData);
            return;
        }// end function

        public function getData() : Object
        {
            return this.data;
        }// end function

        public function onRegister() : void
        {
            return;
        }// end function

        public function stopDelayTimer() : void
        {
            _delayTimer.stop();
            return;
        }// end function

        public function setData(param1:Object) : void
        {
            this.data = param1;
            return;
        }// end function

        private function playProgressHlr() : void
        {
            var _loc_1:Object = {totalTime:playerStatus.totalTime, playTime:playerStatus.playTime, playTimePos:playerStatus.playTimePos, loadTimePos:playerStatus.loadTimePos};
            if (playerStatus.totalTime > 0 && playerStatus.playTime > 0 && nearCompleteTime >= playerStatus.totalTime - playerStatus.playTime)
            {
                sendNearComplete();
            }
            sendNotification(PlayerCommandSet.UPDATE, _loc_1);
            return;
        }// end function

        public function closePreview() : void
        {
            previewPlayer.close();
            return;
        }// end function

        public function get hasReady() : Boolean
        {
            return player.hasReady;
        }// end function

        private function loadProgressHlr() : void
        {
            if (!_isLoadComplete && playerStatus.loadSizePos == 1)
            {
                _isLoadComplete = true;
                sendNotification(PlayerCommandSet.VIDEO_LOAD_COMPLETE);
            }
            if (!_isRequestComplete && playerStatus.loadSize > 0)
            {
                _isRequestComplete = true;
                addNodeLog(NodeLog.VIDEO_REQUEST_COMPLETE);
            }
            var _loc_1:* = playerStatus.loadTime - playerStatus.playTime;
            if (_loc_1 >= PlayerConsts.AD_PRECACHE_DELAY_TIME)
            {
                if (!_fullCached)
                {
                    _fullCached = true;
                    sendNotification(PlayerCommandSet.FULL_CACHED);
                }
            }
            else
            {
                _fullCached = false;
            }
            return;
        }// end function

        protected function changeToNextVideo() : void
        {
            _videoInfo.next(_rateType);
            var _loc_1:* = currVideoNodeInfo;
            if (_debugURL)
            {
                _loc_1.url = _debugURL;
            }
            player.reLoadPlay(_loc_1.url, null);
            player.bandCtrl = _loc_1.bandCtrl;
            return;
        }// end function

        public function onRemove() : void
        {
            return;
        }// end function

        private function previewReadyHlr(event:Event) : void
        {
            sendNotification(PlayerCommandSet.PREVIEW_READY);
            return;
        }// end function

        override protected function statusChangeHlr(event:PlayerEvent) : void
        {
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_2:* = playerStatus.videoStatus.status;
            showBuffer = false;
            onBuffer = false;
            showBufferCount = SHOW_BUFFER_MAX_COUNT;
            switch(_loc_2)
            {
                case VideoStatus.PLAYING:
                {
                    break;
                }
                case VideoStatus.PAUSED:
                {
                    break;
                }
                case VideoStatus.COMPLETED:
                {
                    completeHlr();
                    break;
                }
                case VideoStatus.BUFFERING:
                {
                    break;
                }
                case VideoStatus.READY:
                {
                    bufferFullHlr();
                    break;
                }
                case VideoStatus.ERROR:
                {
                    _loc_3 = playerStatus.playURL;
                    if (_loc_3)
                    {
                        _loc_4 = encodeURIComponent(_loc_3);
                        addNodeLog(NodeLog.VIDEO_REQUEST_ERROR, [_loc_4]);
                        changeVideoCount = CHANGE_VIDEO_MAX_COUNT;
                    }
                    FastTudouTracker.send(pageInfo, playerStatus.cdnURL, playerStatus.playTime, "ie");
                    errorHlr();
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            sendNotification(PlayerCommandSet.NOTIFY_SKIN_STATE, _loc_2);
            return;
        }// end function

        override public function reset() : void
        {
            super.reset();
            closePlayer();
            hasSendNearComplete = false;
            hasFirstMetadata = false;
            sendNotification(PlayerCommandSet.PLAYER_CHANGE, this);
            return;
        }// end function

        public function preview(param1:Number) : void
        {
            previewPlayer.preview(param1);
            return;
        }// end function

        protected function streamBufferEmptyHlr(event:PlayerEvent) : void
        {
            sendNotification(PlayerCommandSet.PLAYER_EVENT, event.type);
            return;
        }// end function

        protected function streamBufferFullHlr(event:PlayerEvent) : void
        {
            sendNotification(PlayerCommandSet.PLAYER_EVENT, event.type);
            return;
        }// end function

        override public function replay() : void
        {
            playerEventTracker.sendStartEventTrack(URLUtil.getDomainName(playerStatus.playURL), playerStatus.totalTime);
            player.replay();
            return;
        }// end function

        private function playerTimerHlr(event:Event) : void
        {
            loadProgressHlr();
            timeOutHlr();
            playProgressHlr();
            return;
        }// end function

        private function timeOutHlr() : void
        {
            if (_delayTimer.running && _delayTime <= _delayTimer.currentCount * _delayTimer.delay && !playerStatus.hasStartPlay)
            {
                _delayTimer.stop();
                addNodeLog(NodeLog.VIDEO_TIME_OUT_PLAY, [_delayTime.toFixed(2)]);
                sendNotification(PlayerCommandSet.TIMEOUT_START_PLAY);
            }
            if (playerStatus.hasLoading && playerStatus.loadSize == 0 && playerStatus.playURL)
            {
                if (changeVideoCount <= 0)
                {
                    addNodeLog(NodeLog.VIDEO_REQUEST_TIMEOUT);
                    changeVideoCount = CHANGE_VIDEO_MAX_COUNT;
                    FastTudouTracker.send(pageInfo, playerStatus.cdnURL, playerStatus.playTime, "to");
                    changeToNextVideo();
                }
                else
                {
                    var _loc_2:* = changeVideoCount - 1;
                    changeVideoCount = _loc_2;
                }
            }
            if (playerStatus.isStartPlay && !showFailure && playerStatus.playURL && playerStatus.loadSize > 0)
            {
                if (showFailCount <= 0)
                {
                    showFailure = true;
                    if (_videoInfo)
                    {
                        addNodeLog(NodeLog.VIDEO_PLAY_REQUEST_TIMEOUT);
                        sendNotification(PlayerCommandSet.SHOW_FAILURE, player.isFastTudouConnect);
                    }
                }
                else
                {
                    var _loc_2:* = showFailCount - 1;
                    showFailCount = _loc_2;
                }
            }
            if (playerStatus.isBuffering && !showBuffer)
            {
                if (onBuffer)
                {
                    if (--showBufferCount < 0)
                    {
                        showBuffer = true;
                        sendNotification(PlayerCommandSet.SHOW_BUFFER);
                    }
                }
                else
                {
                    onBuffer = true;
                    if (++bufferCount >= BUFFER_MAX_COUNT)
                    {
                        showBuffer = true;
                        sendNotification(PlayerCommandSet.SHOW_BUFFER);
                    }
                }
            }
            return;
        }// end function

        private function sendNearComplete() : void
        {
            if (hasSendNearComplete)
            {
                return;
            }
            hasSendNearComplete = true;
            sendNotification(PlayerCommandSet.VIDEO_NEAR_FINISH);
            return;
        }// end function

        private function completeHlr() : void
        {
            sendNearComplete();
            PlayTimeJsTracker.ins.notifyJs(_iBaseInfo.iid, -2);
            sendNotification(PlayerCommandSet.VIDEO_FINISH);
            return;
        }// end function

        protected function skipHeadHlr(event:PlayerEvent) : void
        {
            sendNotification(PlayerCommandSet.SKIPPING_HEAD);
            return;
        }// end function

        override public function close() : void
        {
            super.close();
            closePlayer();
            return;
        }// end function

        private function closePlayer() : void
        {
            _isRequestComplete = false;
            _isLoadComplete = false;
            _fullCached = false;
            showFailure = false;
            changeVideoCount = CHANGE_VIDEO_MAX_COUNT;
            showFailCount = SHOW_FAIL_MAX_COUNT;
            bufferCount = 0;
            _delayTime = 0;
            _delayTimer.stop();
            playerTimer.reset();
            playerTimer.start();
            return;
        }// end function

        public function getProxyName() : String
        {
            return NAME;
        }// end function

    }
}
